//
// GPSMapEdit
// (c) Konstantin Galichsky (kg@geopainting.com), 2006-2007
//
// RG node properties dialog.
//

# include "StdAfx.h"
# include "Resource.h"
# include "NodeProperties.h"
# include "PropertyPages.h"
# include "Map.h"
# include "Types.h"
# include "MapView.h"
# include "Settings.h"
# include "Wrappers.h"
# include "Globals.h"

void ShowNodeProperties (HWND _hWnd, rgn_t & _rgn, size_t _cElement, size_t _cPoint) {
	CCritSec::CLock lock (g_cs);

	CNodeProperties np;
	np.pRgn     = & _rgn;
	np.cElement = _cElement;
	np.cPoint   = _cPoint;

	np.AddPage (new CNodePage);
	if (_rgn.IsRoad ()) {
		np.AddPage (new CNodeRoutingPage);

		if (g_map.pTypeSet->dwFeatures & CTypeSet::tsfHasRoadAddress)
			np.AddPage (new CNodeNumbersPage);
	}

	np.ShowModal (_hWnd, IDS_NODE_PROPERTIES, GetSetting ("NodePropertiesPage", (DWORD) 0));
}

/////////////////////////////////////////////////////

# include "MovePoint.h"
# include "Types.h"
# include "PrintUnits.h"
# include "EditHelpers.h"

bool CNodePage::OnInit (HWND _hwndFocus) {
	CNodeProperties * const pData = static_cast<CNodeProperties *> (GetPP ());

	if (pData->cElement < pData->pRgn->elements.size ()) {
		const rgn_element_t & element = pData->pRgn->elements [pData->cElement];
		if (pData->cPoint < element.points.size ()) {
			const point_t & point = element.points [pData->cPoint];

			char buf [20];

			// Latitude.
			PrintLat (buf, point.y);
			SetDlgItemText (IDC_LAT, buf);

			// Longitude.
			PrintLon (buf, point.x);
			SetDlgItemText (IDC_LON, buf);
		}

		// Ordinal.
		SetDlgItemUInt (IDC_ORDINAL, pData->cPoint);
	}

	return true;
}

void CNodePage::OnCommand (WORD _wCmd, WORD _wNotifyCode, HWND _hwndCtl) {
	switch (_wCmd) {
		case IDC_LAT:
		case IDC_LON: {
			if (_wNotifyCode == EN_CHANGE) {
				const bool bValid = 
					ValidateAsArc (GetDlgItem (IDC_LON)) &&
					ValidateAsArc (GetDlgItem (IDC_LAT));

				EnableDlgItem (IDOK, bValid);
			} else if (_wNotifyCode == EN_UPDATE)
				PropSheetChanged ();
			break;
		}
	}
}

void CNodePage::OnActivatePage (const PSHNOTIFY *) {
	SaveSetting ("NodePropertiesPage", (DWORD) 0);
}

void CNodePage::OnApply (const PSHNOTIFY *) {
	CNodeProperties * const pData = static_cast<CNodeProperties *> (GetPP ());

	if (pData->cElement < pData->pRgn->elements.size ()) {
		rgn_element_t & element = pData->pRgn->elements [pData->cElement];
		if (pData->cPoint < element.points.size ()) {
			point_t point;
			point.x = GetTextAsArc (GetDlgItem (IDC_LON));
			point.y = GetTextAsArc (GetDlgItem (IDC_LAT));

			g_map.pTypeSet->pfRoundPoint (g_map, element.cLevel, point);

			MovePoint (pData->pRgn, pData->cElement, pData->cPoint, point);

			Refresh ();
		}
	}
}
